<?php

namespace app\admin\service;

use app\sys\model\StaffActionLog;
use think\facade\Cache;
use think\facade\Request;

class ServiceTime
{
    // 请求时间
    public static function HttpTime($staff_number): string
    {
        $m = 30;
        $url = Request::baseUrl();
        //30秒内只能操作一次
        $cnt = StaffActionLog::where("staff_no", $staff_number)
            ->where("url", $url)
            ->where("response_status", "=", 1)
            ->where("create_time", ">",time() - $m)
            ->count();
        if ($cnt > 0) {
            return "{$m}秒内只能操作一次";
        }
        return "";
    }

    // 请求次数
    public static function LoginNum($prefix,$staff_number): string
    {
        $loginM = 6;
        // 30分钟
        $expire =  1800;
        // 验证
        $loginNUm = Cache::get("{$prefix}{$staff_number}");
        if ($loginNUm > 0){
            Cache::set("{$prefix}{$staff_number}",$loginNUm+1,$expire);
            $loginNUm = $loginNUm+1 ;
        }else{
            Cache::set("{$prefix}{$staff_number}",1,$expire);
        }

        if ($loginNUm > 5){
            return "请30分钟后重新登录";
        }else{
            $num = $loginM - $loginNUm;
            return "登录失败~,还有{$num}登录机会！";
        }
    }

}